package com.xbongbong.paas.service.dynamic.crm.strategy.impl;

import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.domain.entity.PaasFormEntity;
import com.xbongbong.paas.log.constant.CrmLogMemoConstant;
import com.xbongbong.paas.pojo.dto.FormDataDeleteBatchDTO;
import com.xbongbong.paas.pojo.dto.FormDataUpdateDTO;
import com.xbongbong.paas.pojo.dto.SaasFormSaveDTO;
import com.xbongbong.paas.service.dynamic.crm.strategy.AbstractDynamicStrategy;
import com.xbongbong.parent.interfaces.DynamicStrategy;
import com.xbongbong.paas.service.dynamic.crm.strategy.MainBusinessDynamicStrategy;
import com.xbongbong.pro.customercommunicate.pojo.dto.DynamicDTO;
import com.xbongbong.pro.dynamic.pojo.dto.DynamicArchiveDTO;
import com.xbongbong.pro.dynamic.pojo.dto.DynamicCheckJoinTeamDTO;
import com.xbongbong.pro.dynamic.pojo.dto.DynamicDeleteDTO;
import com.xbongbong.pro.dynamic.pojo.dto.DynamicLabelDTO;
import com.xbongbong.pro.dynamic.pojo.dto.DynamicRefInfoDTO;
import com.xbongbong.pro.dynamic.pojo.dto.DynamicSaveDTO;
import com.xbongbong.pro.dynamic.pojo.dto.DynamicStageJumpDTO;
import com.xbongbong.pro.dynamic.pojo.dto.DynamicUpdateBatchDTO;
import com.xbongbong.pro.dynamic.pojo.dto.DynamicUpdateDTO;
import com.xbongbong.pro.formdata.pojo.vo.UpdateVO;
import com.xbongbong.saas.enums.ProjectLogEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ContactEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *联系人
 *
 *@author weijianxun
 */
@Service("contactDynamicStrategyImpl")
public class ContactDynamicStrategyImpl extends AbstractDynamicStrategy implements DynamicStrategy, MainBusinessDynamicStrategy {

    private final Logger LOG = LoggerFactory.getLogger(ContactDynamicStrategyImpl.class);

    private static final Map<String, Integer> refTextToLogTypeMap = new HashMap<>();

    static {
        refTextToLogTypeMap.put(ContactEnum.CUSTOMER_NAME.getAttr(), ProjectLogEnum.CUSTOMER_REF.getSubType());
    }

    @Override
    public List<Integer> type() {
        return Arrays.asList(XbbRefTypeEnum.CONTACT.getCode());
    }

    public static Map<String, Integer> getRefTextToLogTypeMap(){
        return Collections.unmodifiableMap(refTextToLogTypeMap);
    }
    @Override
    @Async("dynamicThreadPool")
    public void save(DynamicSaveDTO dynamicSaveDTO) {
        try {
            Long dataId = dynamicSaveDTO.getDataId();
            Long formId = dynamicSaveDTO.getFormId();
            SaasFormSaveDTO saasFormSaveDTO = dynamicSaveDTO.getSaasFormSaveDTO();
            String corpid = saasFormSaveDTO.getCorpid();
            PaasFormEntity paasForm = getPaasForm(corpid, formId, saasFormSaveDTO.getBusinessType());
            String contactName = saasFormSaveDTO.getNewData().getString(ContactEnum.NAME.getAttr());
            // 上游操作
            saveRefUpDynamic(dynamicSaveDTO, refTextToLogTypeMap, paasForm, DynamicRefInfoDTO.initDynamicRefInfoDTO(dataId, saasFormSaveDTO.getBusinessType(), contactName), ProjectLogEnum.CONTACT_CREATE.getSubType());
        } catch (Exception e) {
            LOG.error("com.xbongbong.paas.service.dynamic.crm.strategy.impl.ContactDynamicStrategyImpl.save", e);
        }
    }

    @Override
    @Async("dynamicThreadPool")
    public void update(DynamicUpdateDTO dynamicUpdateDTO) {
        try {
            UpdateVO updateVO = dynamicUpdateDTO.getUpdateVO();
            FormDataUpdateDTO formDataUpdateDTO = dynamicUpdateDTO.getFormDataUpdateDTO();
            PaasFormDataEntity newPaasFormDataEntity = updateVO.getPaasFormDataEntity();
            PaasFormEntity paasForm = getPaasForm(formDataUpdateDTO.getCorpid(), newPaasFormDataEntity.getFormId(), formDataUpdateDTO.getBusinessType());
            DynamicDTO dynamicDTO = initDynamicDTO(updateVO.getUserVO().getUserId(), CrmLogMemoConstant.DYNAMIC_USER_UPDATE_BASE_INFO, ProjectLogEnum.CONTACT_UPDATE.getSubType(),
                    DynamicDTO.initMemoHolder(updateVO.getUserVO().getName(), paasForm.getName()), paasForm, formDataUpdateDTO.getSaasMark(), newPaasFormDataEntity.getId());
            insertDynamic(Collections.singletonList(dynamicDTO), formDataUpdateDTO.getCorpid());
        } catch (Exception e) {
            LOG.error("com.xbongbong.paas.service.dynamic.crm.strategy.impl.ContactDynamicStrategyImpl.update", e);
        }
    }

    @Override
    @Async("dynamicThreadPool")
    public void batchUpdate(DynamicUpdateBatchDTO dynamicUpdateBatchDTO) {
        try {
            super.batchUpdate(dynamicUpdateBatchDTO, ProjectLogEnum.CONTACT_UPDATE.getSubType());
        } catch (Exception e) {
            LOG.error("com.xbongbong.paas.service.dynamic.crm.strategy.impl.ContactDynamicStrategyImpl.batchUpdate", e);
        }
    }

    @Override
    @Async("dynamicThreadPool")
    public void batchDelete(DynamicDeleteDTO dynamicDeleteDTO) {
        try {
            FormDataDeleteBatchDTO formDataDeleteBatchDTO = dynamicDeleteDTO.getFormDataDeleteBatchDTO();
            String corpid = formDataDeleteBatchDTO.getCorpid();
            PaasFormEntity paasForm = getPaasForm(corpid, formDataDeleteBatchDTO.getFormId(), formDataDeleteBatchDTO.getBusinessType());
            super.batchRefSaveUpDynamicByDeleteBatch(dynamicDeleteDTO, refTextToLogTypeMap, paasForm, ContactEnum.NAME.getAttr(), ProjectLogEnum.CONTACT_DELETE.getSubType());
        } catch (Exception e) {
            LOG.error("com.xbongbong.paas.service.dynamic.crm.strategy.impl.ContactDynamicStrategyImpl.batchDelete", e);
        }
    }

    @Override
    @Async("dynamicThreadPool")
    public void checkJoinTeam(DynamicCheckJoinTeamDTO dynamicCheckJoinTeamDTO) {
        try {
            super.checkJoinTeam(dynamicCheckJoinTeamDTO, ProjectLogEnum.CONTACT_TEAM_CREATE.getSubType());
        } catch (Exception e) {
            LOG.error("com.xbongbong.paas.service.dynamic.crm.strategy.impl.ContactDynamicStrategyImpl.checkJoinTeam", e);
        }
    }

    @Override
    @Async("dynamicThreadPool")
    public void label(DynamicLabelDTO dynamicLabelDTO) {
        try {
            super.label(dynamicLabelDTO, ProjectLogEnum.CONTACT_UPDATE.getSubType());
        } catch (Exception e) {
            LOG.error("com.xbongbong.paas.service.dynamic.crm.strategy.impl.ContactDynamicStrategyImpl.label", e);
        }
    }

    @Override
    public void stageJump(DynamicStageJumpDTO dynamicStageJumpDTO) {

    }

    @Override
    public void archive(DynamicArchiveDTO dynamicArchiveDTO) {

    }
}
